{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name '__file__' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[1], line 5\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mimportlib\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m reload\n\u001b[0;32m      4\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mos\u001b[39;00m\u001b[38;5;241m,\u001b[39m\u001b[38;5;21;01msys\u001b[39;00m\n\u001b[1;32m----> 5\u001b[0m project_root \u001b[38;5;241m=\u001b[39m os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mdirname(os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mdirname(os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mabspath(\u001b[38;5;18m__file__\u001b[39m)))\n\u001b[0;32m      6\u001b[0m sys\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mappend(project_root)\n\u001b[0;32m      7\u001b[0m \u001b[38;5;28mprint\u001b[39m(sys\u001b[38;5;241m.\u001b[39mpath)\n",
      "\u001b[1;31mNameError\u001b[0m: name '__file__' is not defined"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "from importlib import reload\n",
    "\n",
    "import os,sys\n",
    "project_root = os.path.dirname(os.path.dirname(os.path.abspath()))\n",
    "sys.path.append(project_root)\n",
    "print(sys.path)\n",
    "\n",
    "from htbacktrader_portfoliostrategy import BacktestingEngine\n",
    "from htbacktrader_core.trader.constant import Interval\n",
    "from htbacktrader_core.trader.optimize import OptimizationSetting\n",
    "\n",
    "import htbacktrader_portfoliostrategy.strategies.pair_trading_strategy as stg\n",
    "from htbacktrader_portfoliostrategy.strategies.pair_trading_strategy import PairTradingStrategy\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "engine = BacktestingEngine()\n",
    "engine.set_parameters(\n",
    "    vt_symbols=[\"y888.DCE\", \"p888.DCE\"],\n",
    "    interval=Interval.MINUTE,\n",
    "    start=datetime(2019, 1, 1),\n",
    "    end=datetime(2020, 4, 30),\n",
    "    rates={\n",
    "        \"y888.DCE\": 0/10000,\n",
    "        \"p888.DCE\": 0/10000\n",
    "    },\n",
    "    slippages={\n",
    "        \"y888.DCE\": 0,\n",
    "        \"p888.DCE\": 0\n",
    "    },\n",
    "    sizes={\n",
    "        \"y888.DCE\": 10,\n",
    "        \"p888.DCE\": 10\n",
    "    },\n",
    "    priceticks={\n",
    "        \"y888.DCE\": 1,\n",
    "        \"p888.DCE\": 1\n",
    "    },\n",
    "    capital=1_000_000,\n",
    ")\n",
    "\n",
    "setting = {\n",
    "    \"boll_window\": 20,\n",
    "    \"boll_dev\": 1,\n",
    "}\n",
    "engine.add_strategy(PairTradingStrategy, setting)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "engine.load_data()\n",
    "engine.run_backtesting()\n",
    "df = engine.calculate_result()\n",
    "engine.calculate_statistics()\n",
    "engine.show_chart()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "setting = OptimizationSetting()\n",
    "setting.set_target(\"sharpe_ratio\")\n",
    "setting.add_parameter(\"boll_window\", 10, 30, 1)\n",
    "setting.add_parameter(\"boll_dev\", 1, 3, 1)\n",
    "\n",
    "engine.run_ga_optimization(setting)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "engine.run_bf_optimization(setting)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
